package com.example.mapping.manage;

import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.example.mapping.common.exception.MappingBizException;
import com.example.mapping.constant.Constants;
import com.example.mapping.convert.RuleConvert;
import com.example.mapping.convert.ValueMappingConvert;
import com.example.mapping.domain.Rule;
import com.example.mapping.domain.ValueMapping;
import com.example.mapping.dto.RuleDto;
import com.example.mapping.dto.RuleQueryDto;
import com.example.mapping.dto.ValueMappingDto;
import com.example.mapping.dto.ValueMappingQueryDto;
import com.example.mapping.sevice.RuleService;
import com.example.mapping.sevice.ValueMappingService;
import com.example.mapping.vo.RuleVo;
import com.example.mapping.vo.ValueMappingVo;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Objects;

import static com.example.mapping.common.exception.MappingErrorCodeEnum.*;

/**
 * 值映射manage类
 *
 * @author baoyoujia
 */
@Component
public class ValueMappingManage {

    private final ValueMappingService valueMappingService;

    private final ValueMappingConvert valueMappingConvert;

    public ValueMappingManage(ValueMappingService valueMappingService, ValueMappingConvert valueMappingConvert) {
        this.valueMappingService = valueMappingService;
        this.valueMappingConvert = valueMappingConvert;
    }

    public PageDTO<ValueMappingVo> getValueByPage(ValueMappingQueryDto valueMappingQuery) {
        PageDTO<ValueMapping> pageDTO = new PageDTO<>();
        pageDTO.setCurrent(valueMappingQuery.getCurrent());
        pageDTO.setSize(valueMappingQuery.getSize());
        PageDTO<ValueMapping> valueMappingPage = valueMappingService.page(pageDTO);
        if (CollectionUtils.isEmpty(valueMappingPage.getRecords())) {
            return new PageDTO<>();
        }
        return valueMappingConvert.modelPage2Vo(valueMappingPage);
    }

    public ValueMappingVo getValueMapping(Long id) {
        ValueMapping rule = valueMappingService.getById(id);
        if (Objects.isNull(rule)) {
            return new ValueMappingVo();
        }
        return valueMappingConvert.model2Vo(rule);
    }

    public Boolean addValueMapping(ValueMappingDto valueMappingDto) {
        ValueMapping rule = valueMappingConvert.dto2Model(valueMappingDto);
        boolean result = valueMappingService.save(rule);
        if (!result) {
            throw new MappingBizException(ADD_RULE_ERROR);
        }
        return true;
    }

    public Boolean editValueMapping(ValueMappingDto valueMappingDto) {
        ValueMapping rule = valueMappingConvert.dto2Model(valueMappingDto);
        boolean result = valueMappingService.updateById(rule);
        if (!result) {
            throw new MappingBizException(EDIT_RULE_ERROR);
        }
        return true;
    }

    public Boolean delValueMapping(Long id) {
        ValueMapping valueMapping = valueMappingService.getById(id);
        valueMapping.setSysDelete(Constants.Number.INT_2);
        boolean result = valueMappingService.updateById(valueMapping);
        if (!result) {
            throw new MappingBizException(DEL_RULE_ERROR);
        }
        return true;
    }
}
